package com.cskaoyan.bean;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

//商品list页面参数的封装
@Data
@NoArgsConstructor
public class GoodsParameter {
    private Integer page;
    private Integer limit;
    private String sort;
    private String order;
    //private Integer offset; // (page-1)*limit
    private String goodsSn;
    private String name;
    private Integer goodsId;

    //其为静态成员变量可以用来全局进行访问
    public static GoodsParameter init(Map<String,String[]> parameterMap) {
        //传入的parameterMap集合专门用来请求参数中提供的存储key-value类型的数据
        //Map<String, String[]> parameterMap = request.getParameterMap();
        GoodsParameter goodsParameter = new GoodsParameter();
        try {
            //针对key=value类型请求参数,使用该现成的工具的BeanUtils.copyProperties(instance,request. parameterMap)对请求参数进行封装的赋值
            //把第二个参数的属性赋值给第一个参数;instance中要封装request.parameterMap中可以对应有的成员变量
            BeanUtils.copyProperties(goodsParameter, parameterMap);
            //原本请求参数中没有的成员变量也通过在类中计算进行赋值
            //goodsParameter.setOffset((goodsParameter.getPage() - 1) * goodsParameter.getLimit());
            return goodsParameter;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

}
